package com.hongxin.nw.netty.model.analysis.info;

import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.FieldAnalysis;
import com.hongxin.nw.netty.values.TypeIdentif;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark A.26远程启动充电执行结果
 * @author: GuoXiaolin
 * @date 2019/5/28 9:39
 * @Version 1.0
 * @Descibe
 */
@Slf4j
@Getter
@Setter
public class Mrena1_13 {

    private String co;//    充电设备编号	压缩BCD码	8Byte	充电设备编号
    private String chargeIdentify;//    充电接口标识	BIN码	1Byte	充电设备为一桩多充时用来标记接口号，一桩一充时此项为0。多个接口时顺序对每个接口进行编号
    private String tranNum;//    交易流水号	压缩BCD码	16Byte	32位交易代码
    private String bId;//    计费模型ID	BIN码	8Byte
    private String chargeStartType;//    充电启动方式	BIN码	1Btye	0：远程启动； 1刷卡启动
    private String excRes;//    执行结果	BIN码	1Btye	0：确认 1：成功；2：失败
    private String exceptionReason; //异常原因	BIN码	1Btye  0:设备未连接；1：设备连接故障 2：通信故障 3：SM4密钥错误 4：其他
    private String highestVoltage;//    最高允许充电总电压	BIN码	2Byte	单位:V 精确到小数点后一位
    private String highestElectric ;//    最高允许充电总电流	BIN码	2Byte	单位:A 精确到小数点后一位
    private String totalEnergy;//    电池标称总能量	BIN码	2Byte	单位:kw.h 精确到小数点后一位
    private String highestTemperature;//    最高充电温度	BIN码	1Byte	单位:℃
    private String monomerHighestVoltage;//    单体电池允许最高充电电压	BIN码	2Byte	单位:V 精确到小数点后两位
    private String voltage;//    当前电池整车电压	BIN码	2Byte	单位:V 精确到小数点后一位
    private String physicalCard;//    用户身份ID/物理卡号	压缩BCD码	 8Byte

    private int idx = 0;
    private ByteBuf byteBuf;

    private Mrena1_13 subCo() {
//        this.co = substring(TypeIdentif.BYTE_CO.getbyteLength());
//        this.co = FieldAnalysis.analysisBCD(this.co);
//        return this;

//        int byteLength = TypeIdentif.BYTE_CO.getInt();
//        this.co = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
//        this.co = FieldAnalysis.analysisBCD(this.co);
//        this.idx += byteLength;

        this.co = FieldAnalysis.subCo(TypeIdentif.BYTE_CO.getInt(),idx,byteBuf);
        this.idx += TypeIdentif.BYTE_CO.getInt();
        return this;
    }

    private Mrena1_13 subChargeIdentify() {
//        this.chargeIdentify = substring(TypeIdentif.BYTE_CHARGE_INTERFACE_IDENTIFY.getbyteLength());
//        this.chargeIdentify = FieldAnalysis.analysisBin(this.chargeIdentify);
//        return this;
//        int byteLength = TypeIdentif.BYTE_CHARGE_INTERFACE_IDENTIFY.getInt();
//        this.chargeIdentify = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
//        this.chargeIdentify = FieldAnalysis.analysisBin(this.chargeIdentify);
//        this.idx += byteLength;


        this.chargeIdentify = FieldAnalysis.subGun(TypeIdentif.BYTE_CHARGE_INTERFACE_IDENTIFY.getInt(),idx,byteBuf);
        this.idx += TypeIdentif.BYTE_CHARGE_INTERFACE_IDENTIFY.getInt();
        return this;
    }

    private Mrena1_13 subTranNum() {
//        this.tranNum = substring(TypeIdentif.BYTE_TRAN_NUM.getbyteLength());
//        this.tranNum = FieldAnalysis.analysisBCD(this.tranNum);
//        return this;
//        int byteLength = TypeIdentif.BYTE_TRAN_NUM.getInt();
//        this.tranNum = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
//        this.tranNum = FieldAnalysis.analysisBCD(this.tranNum);
//        this.idx += byteLength;


        this.tranNum = FieldAnalysis.subTranNum(TypeIdentif.BYTE_TRAN_NUM.getInt(),idx,byteBuf);
        this.idx += TypeIdentif.BYTE_TRAN_NUM.getInt();
        return this;
    }

    private Mrena1_13 subJmid() {
//        this.jmid = substring(TypeIdentif.BYTE_BILLING_MODEL_ID.getbyteLength());
//        this.jmid = FieldAnalysis.analysisBCD(this.jmid);
//        return this;
//        int byteLength = TypeIdentif.BYTE_BILLING_MODEL_ID.getInt();
//        this.bId = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
//        this.bId = FieldAnalysis.analysisBCD(this.bId);
//        this.idx += byteLength;


        this.bId = FieldAnalysis.subBill(TypeIdentif.BYTE_BILLING_MODEL_ID.getInt(),idx,byteBuf);
        this.idx += TypeIdentif.BYTE_BILLING_MODEL_ID.getInt();
        return this;
    }

    private Mrena1_13 subChargeStartType() {
//        this.chargeStartType = substring(2);
//        this.chargeStartType = FieldAnalysis.analysisBin(this.chargeStartType);
//        return this;
        int byteLength = TypeIdentif.BYTE_CHARGE_START_TYPE.getInt();
        this.chargeStartType = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.chargeStartType = FieldAnalysis.analysisBin(this.chargeStartType);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subExcuRes() {
//        this.excuRes = substring(TypeIdentif.BYTE_EXECUTE_RES.getbyteLength());
//        this.excuRes = FieldAnalysis.analysisBin(this.excuRes);
//        return this;
        int byteLength = TypeIdentif.BYTE_EXECUTE_RES.getInt();
        this.excRes = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.excRes = FieldAnalysis.analysisBin(this.excRes);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subExceptionReason() {
//        this.exceptionReason = substring(TypeIdentif.BYTE_EXCEPTION_REASON.getbyteLength());
//        this.exceptionReason = FieldAnalysis.analysisBin(this.exceptionReason);
//        return this;
        int byteLength = TypeIdentif.BYTE_EXCEPTION_REASON.getInt();
        this.exceptionReason = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.exceptionReason = FieldAnalysis.analysisBin(this.exceptionReason);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subHighestVoltage() {
//        this.highestVoltage = substring(TypeIdentif.BYTE_HIGHEST_VOLTAGE.getbyteLength());
//        this.highestVoltage = FieldAnalysis.hexToDecimal(this.highestVoltage, 1);
//        return this;
        int byteLength = TypeIdentif.BYTE_HIGHEST_VOLTAGE.getInt();
        this.highestVoltage = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.highestVoltage = FieldAnalysis.hexToDecimal(this.highestVoltage, 1);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subHighestElectric() {
//        this.highestElectric = substring(TypeIdentif.BYTE_HIGHEST_ELECTRIC.getbyteLength());
//        this.highestElectric = FieldAnalysis.hexToDecimal(this.highestElectric, 1);
//        return this;
        int byteLength = TypeIdentif.BYTE_HIGHEST_ELECTRIC.getInt();
        this.highestElectric = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.highestElectric = FieldAnalysis.hexToDecimal(this.highestElectric, 1);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subTotalEnergy() {
//        this.totalEnergy = substring(TypeIdentif.BYTE_TOTAL_ENERGY.getbyteLength());
//        this.totalEnergy = FieldAnalysis.hexToDecimal(this.totalEnergy, 1);
//        return this;
        int byteLength = TypeIdentif.BYTE_TOTAL_ENERGY.getInt();
        this.totalEnergy = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.totalEnergy = FieldAnalysis.hexToDecimal(this.totalEnergy, 1);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subHighestTemperature() {
//        this.highestTemperature = substring(TypeIdentif.BYTE_HIGHEST_TEMPERATURE.getbyteLength());
//        this.highestTemperature = FieldAnalysis.analysisBin(this.highestTemperature);
//        return this;
        int byteLength = TypeIdentif.BYTE_HIGHEST_TEMPERATURE.getInt();
        this.highestTemperature = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.highestTemperature = FieldAnalysis.analysisBin(this.highestTemperature);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subMonomerHighestVoltage() {
//        this.monomerHighestVoltage = substring(TypeIdentif.BYTE_MONOMER_HIGHEST_VOLTAGE.getbyteLength());
//        this.monomerHighestVoltage = FieldAnalysis.hexToDecimal(this.monomerHighestVoltage, 2);
//        return this;
        int byteLength = TypeIdentif.BYTE_MONOMER_HIGHEST_VOLTAGE.getInt();
        this.monomerHighestVoltage = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.monomerHighestVoltage = FieldAnalysis.hexToDecimal(this.monomerHighestVoltage, 2);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subVoltage() {
//        this.voltage = substring(TypeIdentif.BYTE_VOLTAGE.getbyteLength());
//        this.voltage = FieldAnalysis.hexToDecimal(this.voltage, 1);
//        return this;
        int byteLength = TypeIdentif.BYTE_VOLTAGE.getInt();
        this.voltage = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.voltage = FieldAnalysis.hexToDecimal(this.voltage, 1);
        this.idx += byteLength;
        return this;
    }

    private Mrena1_13 subPhysicalCard() {
//        this.physicalCard = substring(TypeIdentif.BYTE_PHYSICS_NUM.getbyteLength());
//        this.physicalCard = FieldAnalysis.analysisBCD(this.physicalCard);
//        return this;

        int byteLength = TypeIdentif.BYTE_PHYSICS_NUM.getInt();
        this.physicalCard = BinaryTools.subByteBuf(byteLength, idx, byteBuf);
        this.physicalCard = FieldAnalysis.analysisBCD(this.physicalCard);
        this.idx += byteLength;
        return this;
    }

    public Mrena1_13 analysisStr(int idx, ByteBuf byteBuf) {
        this.idx = idx;
        this.byteBuf = byteBuf;

        return this.subCo()
                .subChargeIdentify()
                .subTranNum()
                .subJmid()
                .subChargeStartType()
                .subExcuRes()
                .subExceptionReason()
                .subHighestVoltage()
                .subHighestElectric()
                .subTotalEnergy()
                .subHighestTemperature()
                .subMonomerHighestVoltage()
                .subVoltage()
                .subPhysicalCard();
    }
    @Override
    public String toString() {
        return "Mrena1_13{" +
                "co='" + co + '\'' +
                ", chargeIdentify='" + chargeIdentify + '\'' +
                ", tranNum='" + tranNum + '\'' +
                ", bId='" + bId + '\'' +
                ", chargeStartType='" + chargeStartType + '\'' +
                ", excRes='" + excRes + '\'' +
                ", exceptionReason='" + exceptionReason + '\'' +
                ", highestVoltage='" + highestVoltage + '\'' +
                ", highestElectric='" + highestElectric + '\'' +
                ", totalEnergy='" + totalEnergy + '\'' +
                ", highestTemperature='" + highestTemperature + '\'' +
                ", monomerHighestVoltage='" + monomerHighestVoltage + '\'' +
                ", voltage='" + voltage + '\'' +
                ", physicalCard='" + physicalCard + '\'' +
                '}';
    }
}
